home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / Button / Sources / Part.cpp < prev    next >
Encoding:
Text File  |  1996-04-25  |  14.7 KB  |  496 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                Part.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Author:                Henri Lamiraux
  7. //    Modified by:        M.Boetcher to accept Dropped and Pasted sounds
  8. //
  9. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  10. //
  11. //========================================================================================
  12.  
  13. #ifndef PART_H
  14. #include "Part.h"
  15. #endif
  16.  
  17. #ifndef BINDING_K
  18. #include "Binding.k"
  19. #endif
  20.  
  21. #ifndef DEFINES_K
  22. #include "Defines.k"
  23. #endif
  24.  
  25. #ifndef SELECT_H
  26. #include "Select.h"
  27. #endif
  28.  
  29. #ifndef ACTIONS_H
  30. #include "Actions.h"
  31. #endif
  32.  
  33. #ifndef CONTENT_H
  34. #include "Content.h"
  35. #endif
  36.  
  37. // ----- Framework Layer -----
  38.  
  39. #ifndef FWBUTTON_H
  40. #include "FWButton.h"
  41. #endif
  42.  
  43. #ifndef FWPRESEN_H
  44. #include "FWPresen.h"
  45. #endif
  46.  
  47. #ifndef FWNOTDEF_H
  48. #include "FWNotDef.h"
  49. #endif
  50.  
  51. #ifndef FWCONTXT_H
  52. #include "FWContxt.h"
  53. #endif
  54.  
  55. // ----- OS Layer -----
  56.  
  57. #ifndef FWRECSHP_H
  58. #include "FWRecShp.h"
  59. #endif
  60.  
  61. #ifndef FWODGEOM_H
  62. #include "FWODGeom.h"
  63. #endif
  64.  
  65. #ifndef FWEVENT_H
  66. #include "FWEvent.h"
  67. #endif
  68.  
  69. #ifndef FWMEMMGR_H
  70. #include "FWMemMgr.h"
  71. #endif
  72.  
  73. #ifndef FWMENU_H
  74. #include "FWMenu.h"
  75. #endif
  76.  
  77. #ifndef FWBARRAY_H
  78. #include "FWBArray.h"
  79. #endif
  80.  
  81. #ifndef FWCFMRES_H
  82. #include "FWCFMRes.h"
  83. #endif
  84.  
  85. #ifndef FWDRCMD_H
  86. #include "FWDrCmd.h"
  87. #endif
  88.  
  89. // ----- Foundation Layer -----
  90.  
  91. #ifndef FWNOTIFN_H
  92. #include <FWNotifn.h>
  93. #endif
  94.  
  95. #ifndef FWCLAINF_H
  96. #include <FWClaInf.h>
  97. #endif
  98.  
  99. // ----- OpenDoc Includes -----
  100.  
  101. #ifndef FWODTYPS_H
  102. #include "FWODTyps.h"
  103. #endif
  104.  
  105. #ifndef SOM_Module_OpenDoc_StdProps_defined
  106. #include <StdProps.xh>
  107. #endif
  108.  
  109. #ifndef SOM_ODStorageUnit_xh
  110. #include <StorageU.xh>
  111. #endif
  112.  
  113. #ifndef SOM_ODTranslation_xh
  114. #include <Translt.xh>
  115. #endif
  116.  
  117. #ifndef SOM_ODSession_xh
  118. #include <ODSessn.xh>
  119. #endif
  120.  
  121. #ifndef SOM_ODDragItemIterator_xh
  122. #include <DgItmIt.xh>
  123. #endif
  124.  
  125. // ----- Macintosh Includes -----
  126. #if defined(FW_BUILD_MAC) && !defined(__SOUND__)
  127. #include <Sound.h>
  128. #endif
  129.  
  130. //========================================================================================
  131. //    Runtime information
  132. //========================================================================================
  133.  
  134. #ifdef FW_BUILD_MAC
  135. #pragma segment odfbutton
  136. #endif
  137.  
  138. //========================================================================================
  139. //    Constant
  140. //========================================================================================
  141.  
  142. // View ids
  143.  
  144. const ODID kButtonId     = 0;
  145.  
  146. //========================================================================================
  147. //    class CButtonPart
  148. //========================================================================================
  149.  
  150. FW_DEFINE_AUTO(CButtonPart)
  151.     
  152. //----------------------------------------------------------------------------------------
  153. //    CButtonPart::CButtonPart
  154. //----------------------------------------------------------------------------------------
  155.  
  156. CButtonPart::CButtonPart(ODPart* odPart) :
  157.     FW_CPart(odPart, FW_gInstance, kPartInfoID)
  158. {
  159. }
  160.  
  161. //----------------------------------------------------------------------------------------
  162. //    CButtonPart::~CButtonPart
  163. //----------------------------------------------------------------------------------------
  164.  
  165. CButtonPart::~CButtonPart()
  166. {
  167. }
  168.  
  169. //----------------------------------------------------------------------------------------
  170. //    CButtonPart::Initialize
  171. //----------------------------------------------------------------------------------------
  172.  
  173. void CButtonPart::Initialize(Environment* ev)
  174. {
  175.     FW_CPart::Initialize(ev);
  176.     
  177.     // ----- Register our Presentation
  178.     CButtonSelection* selection = 
  179.                             FW_NEW(CButtonSelection, (ev, fButtonContent));
  180.     RegisterPresentation(ev, kODFButtonPresentation, 
  181.                             TRUE, selection);
  182. }
  183.  
  184. //----------------------------------------------------------------------------------------
  185. //    CButtonPart::NewPartContent
  186. //----------------------------------------------------------------------------------------
  187.  
  188. FW_CContent* CButtonPart::NewPartContent(Environment* ev)
  189. {
  190.     fButtonContent = FW_NEW(CButtonContent, (ev, this));
  191.     return fButtonContent;
  192. }
  193.  
  194. //----------------------------------------------------------------------------------------
  195. //    CButtonPart::NewFrame
  196. //----------------------------------------------------------------------------------------
  197.  
  198. FW_CFrame* CButtonPart::NewFrame(Environment* ev,
  199.                                  ODFrame* frame,
  200.                                  FW_CPresentation* presentation,
  201.                                  FW_Boolean storage)
  202. {
  203.     return FW_NEW(CButtonFrame, (ev, frame, presentation, this, kButtonView));
  204. }
  205.  
  206. //----------------------------------------------------------------------------------------
  207. // CButtonPart::ClearPartStorage
  208. //----------------------------------------------------------------------------------------
  209.  
  210. void CButtonPart::ClearPartStorage(Environment* ev, ODStorageUnit* storage)
  211. {
  212.     FW_CPart::ClearPartStorage(ev, storage);
  213.     
  214.     // ----- Remove script value if any -----
  215.     if (storage->Exists(ev, kODPropContents, kScriptScrapKind, 0))
  216.     {
  217.         storage->Focus(ev, kODPropContents, kODPosUndefined, kScriptScrapKind, 0, kODPosUndefined);
  218.         storage->Remove(ev);
  219.     }
  220.     
  221.     // ----- Remove sound value if any -----
  222.     if (storage->Exists(ev, kODPropContents, kSoundScrapKind, 0))
  223.     {
  224.         storage->Focus(ev, kODPropContents, kODPosUndefined, kSoundScrapKind, 0, kODPosUndefined);
  225.         storage->Remove(ev);
  226.     }
  227. }
  228.  
  229. //------------------------------------------------------------------------------
  230. // CButtonPart::IsValidContentValue
  231. //------------------------------------------------------------------------------
  232. //    Because I support more than the standard PartKind I have to test for them
  233.  
  234. FW_Boolean CButtonPart::IsValidContentValue(Environment* ev, ODType type)
  235. {
  236.     if (FW_PrimitiveStringEqual(type, this->GetPartKind(ev)))
  237.         return true;
  238.     else if (FW_PrimitiveStringEqual(type, kScriptScrapKind))
  239.         return true;
  240.     else if (FW_PrimitiveStringEqual(type, kSoundScrapKind))
  241.         return true;
  242.     else 
  243.         return false;
  244. }
  245.  
  246. //----------------------------------------------------------------------------------------
  247. // CButtonPart::DoAction
  248. //----------------------------------------------------------------------------------------
  249.  
  250. void CButtonPart::DoAction()
  251. {
  252.     CAction* action = fButtonContent->GetAction();
  253.     if (action != NULL)
  254.         action->DoIt();
  255. }
  256.  
  257. //========================================================================================
  258. //    class CButtonFrame
  259. //========================================================================================
  260.  
  261. FW_DEFINE_AUTO(CButtonFrame)
  262. FW_DEFINE_CLASS_M2(CButtonFrame, FW_CFrame, FW_MReceiver)
  263.  
  264. //----------------------------------------------------------------------------------------
  265. //    CButtonFrame::CButtonFrame
  266. //----------------------------------------------------------------------------------------
  267.  
  268. CButtonFrame::CButtonFrame(Environment* ev, 
  269.                     ODFrame* frame, 
  270.                     FW_CPresentation* presentation, 
  271.                     CButtonPart* part,
  272.                     FW_ResourceId id) :                            // view resource id
  273.     FW_CFrame(ev, frame, presentation, part, id),
  274.     FW_MDroppableFrame(ev, this),
  275.     fButtonPart(part)
  276. {
  277.     // CButtonFrame maintains its own focus sets instead of using the presentation's default
  278.     // in order to switch between 2 kind of focus in during mouse-down:    
  279.     // - fEmptyFocusSet will be used to handle simple clicks
  280.     // - fFullFocusSet will be used to select the button as an embedded frame
  281.     ODArbitrator* arbitrator = part->GetSession(ev)->GetArbitrator(ev);
  282.     fEmptyFocusSet = arbitrator->CreateFocusSet(ev);    
  283.     fFullFocusSet = arbitrator->CreateFocusSet(ev);
  284.     fFullFocusSet->Add(ev, FW_CPart::gKeyFocusToken);
  285.     fFullFocusSet->Add(ev, FW_CPart::gMenuFocusToken);
  286.     fFullFocusSet->Add(ev, FW_CPart::gSelectionFocusToken);
  287.     fFullFocusSet->Add(ev, FW_CPart::gClipboardFocusToken);    
  288.     
  289.     fFocusSet = fEmptyFocusSet;
  290.     
  291.     ChangeDroppableState(ev, FW_kFrameDroppable);    // Only droppable in view as frame
  292.     
  293.     FW_END_CONSTRUCTOR
  294. }
  295.  
  296. //----------------------------------------------------------------------------------------
  297. //    CButtonFrame::~CButtonFrame
  298. //----------------------------------------------------------------------------------------
  299.  
  300. CButtonFrame::~CButtonFrame()
  301. {
  302.     FW_START_DESTRUCTOR
  303.     
  304.     delete fFullFocusSet;
  305.     delete fEmptyFocusSet;
  306. }
  307.  
  308. //----------------------------------------------------------------------------------------
  309. //    CButtonFrame::PostCreateViewFromStream
  310. //----------------------------------------------------------------------------------------
  311. // PostCreateViewFromStream is called after subviews are created from resources.  
  312. // Add code which cannot be handled by current resource types.
  313.  
  314. void CButtonFrame::PostCreateViewFromStream(Environment* ev)
  315. {
  316.     // ----- Add a behavior to our button
  317.     FW_CView* view = FindViewById(ev, 1);
  318.     FW_CButton* button = FW_DYNAMIC_CAST(FW_CButton, view);
  319.     FW_ASSERT(button != NULL);
  320.  
  321.     //     The role of the COptionBehavior is to detect Option-Click (In this case change the focus
  322.     //    set of the frame). Having a behavior allows us to not have to subclass FW_CButton.
  323.     COptionBehavior* optionBehavior = FW_NEW(COptionBehavior, (ev, this));
  324.     button->AdoptEventHandler(ev, optionBehavior);
  325.  
  326.     // WARNING: Make sure that classes created from resources won't be dead-stripped
  327.     FW_DO_NOT_DEAD_STRIP(FW_CGrowBox);
  328. }
  329.  
  330. //----------------------------------------------------------------------------------------
  331. //    CButtonFrame::CreateSubViews
  332. //----------------------------------------------------------------------------------------
  333.  
  334. // NOTE: This method is not used because the frame resource id was passed to the ctor
  335. //         We left it to show how to create views by program instead of using resources. 
  336. #if 0
  337. void CButtonFrame::CreateSubViews(Environment* ev)
  338. {
  339.     // Create button control with this frame size
  340.     FW_CRect frameRect = this->GetBounds(ev);    
  341.     FW_CButton* button = FW_NEW(FW_CButton, (ev, this, kButtonId, frameRect, FW_kPushButton, 
  342.                             FW_CString("Button")));
  343.     
  344.     // Bind button to keep the same frame size
  345.     button->SetBindings(ev, FW_kFitToEnclosure);
  346.  
  347.     // Connect the frame to this button
  348.     button->LinkControlTo(ev, this);
  349.  
  350.     //     The role of the COptionBehavior is to detect Option-Click (In this case change the focus
  351.     //    set of the frame). Having a behavior allows us to not have to subclass FW_CButton.
  352.     COptionBehavior* optionBehavior = FW_NEW(COptionBehavior, (ev, this));
  353.     button->AdoptEventHandler(ev, optionBehavior);
  354.  
  355.     // Create a GrowBox only in root frame
  356.     if (this->IsRoot(ev)) 
  357.     {
  358.         FW_CPoint sbSize = FW_CScrollBar::GetDefaultScrollBarSize();
  359.         frameRect.right -= sbSize.x;
  360.         frameRect.bottom -= sbSize.y;
  361.         
  362.         FW_CGrowBox* growBox = new FW_CGrowBox(ev, this, 0, frameRect.BotRight());
  363.     }
  364. }
  365. #endif    
  366.  
  367. //----------------------------------------------------------------------------------------
  368. //    CButtonFrame::CanAcceptDrop
  369. //----------------------------------------------------------------------------------------
  370.  
  371. ODDragResult CButtonFrame::CanAcceptDrop(Environment* ev, ODDragItemIterator* dragInfo)
  372. {
  373.     ODDragResult acceptDrop = FW_MDroppableFrame::CanAcceptDrop(ev, dragInfo);
  374.  
  375.     if (!acceptDrop)
  376.     {
  377.         for (ODStorageUnit *dragSU = dragInfo->First(ev); 
  378.                             dragSU != NULL; 
  379.                             dragSU = dragInfo->Next(ev))
  380.         {
  381.             if (CSoundAction::IsInStorage(ev, dragSU))
  382.                 return TRUE;
  383.             if (CScriptAction::IsInStorage(ev, dragSU))
  384.                 return TRUE;
  385.         }
  386.     }
  387.  
  388.     return acceptDrop;
  389. }
  390.  
  391. //----------------------------------------------------------------------------------------
  392. //    CButtonFrame::Draw
  393. //----------------------------------------------------------------------------------------
  394.  
  395. void CButtonFrame::Draw(Environment* ev, ODFacet* facet, ODShape* invalidShape)
  396. {
  397.     FW_CViewContext fc(ev, NULL, facet, invalidShape);
  398.     
  399.     // Just erase before the button draws itself
  400.     FW_CRect invalidRect;
  401.     fc.GetClipRect(invalidRect);
  402.     FW_CRectShape::RenderRect(fc, invalidRect, FW_kFill, FW_kWhiteEraseInk);
  403. }
  404.  
  405. //----------------------------------------------------------------------------------------
  406. //    CButtonFrame::HandleNotification
  407. //----------------------------------------------------------------------------------------
  408.  
  409. void CButtonFrame::HandleNotification(Environment* ev, const FW_CNotification& notification)
  410. {
  411.     if (notification.GetMessage() == FW_kButtonPressedMsg)
  412.     {
  413.         // no need to check for the button Id, there is only 1 button in this frame
  414.         fButtonPart->DoAction();        
  415.         return;
  416.     }
  417. }
  418.  
  419. //----------------------------------------------------------------------------------------
  420. //    CButtonFrame::GetFocusSet
  421. //----------------------------------------------------------------------------------------
  422.  
  423. ODFocusSet* CButtonFrame::GetFocusSet(Environment* ev) const
  424. {
  425.     return fFocusSet;
  426. }
  427.  
  428. //----------------------------------------------------------------------------------------
  429. //    CButtonFrame::SetFocusSet
  430. //----------------------------------------------------------------------------------------
  431.  
  432. void CButtonFrame::SetFocusSet(Environment* ev, FW_Boolean optionPressed)
  433. {
  434.     // Warning: Do not change fFocusSet while the frame is active otherwise the wrong focus 
  435.     //            will be used at the next deactivation
  436.  
  437.     if (HasSelectionFocus(ev) == FALSE) {
  438.         fFocusSet =  optionPressed ? fFullFocusSet : fEmptyFocusSet;
  439.     }
  440. }
  441.  
  442. //----------------------------------------------------------------------------------------
  443. //    CButtonFrame::NewDropCommand
  444. //----------------------------------------------------------------------------------------
  445.  
  446. FW_CDropCommand* CButtonFrame::NewDropCommand(Environment *ev, 
  447.                                     FW_CFrame* frame,
  448.                                     ODDragItemIterator* dropInfo, 
  449.                                     ODFacet* facet, 
  450.                                     const FW_CPoint& dropPoint)
  451. {
  452.  
  453.     return FW_NEW(FW_CDropCommand, (ev, frame, dropInfo, facet, dropPoint, FW_kCantUndo));
  454. }
  455.  
  456. //========================================================================================
  457. //    class COptionBehavior
  458. //========================================================================================
  459.  
  460. FW_DEFINE_AUTO(COptionBehavior)
  461.  
  462. //----------------------------------------------------------------------------------------
  463. //    COptionBehavior::COptionBehavior
  464. //----------------------------------------------------------------------------------------
  465.  
  466. COptionBehavior::COptionBehavior(Environment* ev, CButtonFrame* frame) :
  467.     FW_MEventHandler(ev, NULL, kNoPriority),
  468.     fFrame(frame)
  469. {
  470.     FW_END_CONSTRUCTOR
  471. }
  472.  
  473. //----------------------------------------------------------------------------------------
  474. //    COptionBehavior::~COptionBehavior
  475. //----------------------------------------------------------------------------------------
  476.  
  477. COptionBehavior::~COptionBehavior() 
  478. {
  479.     FW_START_DESTRUCTOR
  480. }
  481.  
  482. //----------------------------------------------------------------------------------------
  483. //    COptionBehavior::DoMouseDown
  484. //----------------------------------------------------------------------------------------
  485.  
  486. FW_Boolean COptionBehavior::DoMouseDown(Environment* ev, const FW_CMouseEvent& theMouseEvent) 
  487. {
  488.     FW_Boolean optionPressed = theMouseEvent.IsCopyModifier(ev);
  489.     
  490.     // Switches focus set on the fly to allow selection of the button when it's an
  491.     // embedded frame.
  492.     fFrame->SetFocusSet(ev, optionPressed);
  493.  
  494.     return optionPressed;
  495. }
  496.